﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EasyErp.Domain;

namespace EasyErp.Controllers
{ 
    public class OperationController : Controller
    {
        private erpEntities db = new erpEntities();

        //
        // GET: /Operation/

        public ViewResult Index()
        {
            var operationinfo = db.OperationInfo.Include(o => o.BaseRateInfo).Include(o => o.FacilityInfo);
            return View(operationinfo.ToList());
        }

        //
        // GET: /Operation/Details/5

        public ViewResult Details(int id)
        {
            OperationInfo operationinfo = db.OperationInfo.Find(id);
            return View(operationinfo);
        }

        //
        // GET: /Operation/Create

        public ActionResult Create()
        {
            ViewBag.BaseRateId = new SelectList(db.BaseRateInfo, "BaseRateId", "JobClass");
            ViewBag.FacilityId = new SelectList(db.FacilityInfo, "FacilityId", "FacilityDescription");
            return View();
        } 

        //
        // POST: /Operation/Create

        [HttpPost]
        public ActionResult Create(OperationInfo operationinfo)
        {
            if (ModelState.IsValid)
            {
                db.OperationInfo.Add(operationinfo);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.BaseRateId = new SelectList(db.BaseRateInfo, "BaseRateId", "JobClass", operationinfo.BaseRateId);
            ViewBag.FacilityId = new SelectList(db.FacilityInfo, "FacilityId", "FacilityDescription", operationinfo.FacilityId);
            return View(operationinfo);
        }
        
        //
        // GET: /Operation/Edit/5
 
        public ActionResult Edit(int id)
        {
            OperationInfo operationinfo = db.OperationInfo.Find(id);
            ViewBag.BaseRateId = new SelectList(db.BaseRateInfo, "BaseRateId", "JobClass", operationinfo.BaseRateId);
            ViewBag.FacilityId = new SelectList(db.FacilityInfo, "FacilityId", "FacilityDescription", operationinfo.FacilityId);
            return View(operationinfo);
        }

        //
        // POST: /Operation/Edit/5

        [HttpPost]
        public ActionResult Edit(OperationInfo operationinfo)
        {
            if (ModelState.IsValid)
            {
                db.Entry(operationinfo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.BaseRateId = new SelectList(db.BaseRateInfo, "BaseRateId", "JobClass", operationinfo.BaseRateId);
            ViewBag.FacilityId = new SelectList(db.FacilityInfo, "FacilityId", "FacilityDescription", operationinfo.FacilityId);
            return View(operationinfo);
        }

        //
        // GET: /Operation/Delete/5
 
        public ActionResult Delete(int id)
        {
            OperationInfo operationinfo = db.OperationInfo.Find(id);
            return View(operationinfo);
        }

        //
        // POST: /Operation/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            OperationInfo operationinfo = db.OperationInfo.Find(id);
            db.OperationInfo.Remove(operationinfo);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}